package com.skyhigh.web.webservice.services;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.skyhigh.web.webservice.domain.CategoriaMercaderia;
import com.skyhigh.web.webservice.domain.CategoriaMercaderiaList;
import com.skyhigh.web.webservice.exceptions.ExceptionHelper;
import com.skyhigh.web.webservice.exceptions.RecordNotFoundException;
import com.skyhigh.web.webservice.exceptions.ServiceException;
import com.skyhigh.web.webservice.persistence.GenericDAO;

@Service("categoriaMercaderiaService")
@Transactional(
    propagation = Propagation.REQUIRED,
    rollbackFor = ServiceException.class,
    isolation = Isolation.DEFAULT,
    readOnly = false)
public class CategoriaMercaderiaServiceImpl implements
		CategoriaMercaderiaService {

	@Autowired
	@Qualifier("genericDao")
	private GenericDAO dao;
	
	protected final Log logger = LogFactory.getLog(getClass());
		
	
	public CategoriaMercaderia agregar(CategoriaMercaderia entity) {
		dao.save(entity);
        return entity;
	}

	
	public void actualizar(CategoriaMercaderia entity) {
		if(dao.exists(entity)){			
    		dao.update(entity);
    	}else{
    		throw ExceptionHelper.newPrimaryKeyNotFountEx();
    	}
	}

	
	public void eliminar(Integer id) {
		try {
			dao.deleteById(CategoriaMercaderia.class, id);
		} catch (RecordNotFoundException e) {
			throw ExceptionHelper.newPrimaryKeyNotFountEx(id);
		}
	}
	
	@Transactional(readOnly = true)
	public CategoriaMercaderia obtenerObjeto(Integer id) {
		CategoriaMercaderia entity = dao.<CategoriaMercaderia>get(CategoriaMercaderia.class, id);
		if(entity == null){
    		throw ExceptionHelper.newPrimaryKeyNotFountEx();			
		}
    	
    	return entity;
	}

	@Transactional(readOnly = true)
	public CategoriaMercaderiaList obtenerObjetos() {
		List<CategoriaMercaderia> entities = dao.getAll(CategoriaMercaderia.class);    	
    	return new CategoriaMercaderiaList(entities);
	}

}
